class_name LoadingView
extends BaseView

#region 属性
## TODO 最小加载时间，后续需要配合检查其它生成是否完成来使用
@export var min_loading_time:float = 4
@export var loading_label:Label
var update_interval:float = 0.3
var dot_count = 0
var update_time = 0
var update_time_remaining = 0
var args:Dictionary = {}
var connect_obj_dict:Dictionary[Signal,ConnectObj] = {}
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_view():
	super.init_view()
	set_process(false)
func on_enter(args:Dictionary = {}):
	super.on_enter(args)
	self.args = args
	_reset()
	set_process(true)
func on_pause(args:Dictionary = {}):
	super.on_pause(args)
	set_process(false)
func on_resume(args:Dictionary = {}):
	super.on_resume(args)
	_reset()
	set_process(true)
func on_exit(args:Dictionary = {}):
	set_process(false)
	for connect_obj:ConnectObj in connect_obj_dict.values():
		if connect_obj:
			connect_obj.disconnect_all()
	super.on_exit(args)
func get_view_state() -> GameEnum.ViewState:
	return super.get_view_state()
#endregion
#region 公共方法
#endregion
#region 私有方法
func _reset():
	dot_count = 0
	update_time = 0
	update_time_remaining = 0
func _get_dot_str() -> String:
	var str = ""
	for i in dot_count:
		str += "."
	return str
#endregion
#region 生命周期函数
func _process(delta: float) -> void:
	update_time += delta
	update_time_remaining += delta
	if update_time >= min_loading_time and args.has("TargetView"):
		## TODO 先暂时在这里处理场景的切入切出
		var fade_in_first_scene_options = GameSceneSystem.create_options(1, "fade_scribbles",0.1,false,"scribbles")
		var first_scene_general_options = GameSceneSystem.create_general_options(Color(0, 0, 0), 1, false)
		GameSceneSystem.show_scene(fade_in_first_scene_options, first_scene_general_options)
		await get_tree().process_frame
		GameViewSystem.open_view(args.get("TargetView"))
		return
	while update_time_remaining >= update_interval:
		update_time_remaining -= update_interval
		dot_count += 1
		dot_count %= 7
	loading_label.text = Constant.LOADING_VIEW_DESC.format([_get_dot_str()])
#endregion
